package com.kn.keywordmatch.common;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class IndexRotator {
    private final List<Integer> indexs;
    private final AtomicInteger currentIndex = new AtomicInteger(0);

    public IndexRotator(List<Integer> indexs) {
        this.indexs = indexs;
    }

    public Integer getNextIndex() {
        int index = currentIndex.getAndUpdate(i -> (i + 1) % indexs.size());
        return indexs.get(index);
    }


    public static void main(String[] args) {
        List<Integer> integers = Arrays.asList(0, 1, 2, 3, 4);
        IndexRotator urlRotator = new IndexRotator(integers);

        for (int i = 0; i < 10; i++) {
            System.out.println(urlRotator.getNextIndex());
        }
    }


}
